Filling listboxes when using mapHandle
When Carl announced the new features in LB4, two of them really caught my attention. These were, mapHandle and Handle Variables. The prospect of having multiple windows, all similar, and only having to code the window once, was perfect for one of my games,(a Yahtzee clone). Being able to manipulate them using variables for the #handle, was just icing on the cake. As soon as the first beta came out I downloaded it and went to work.
I jumped into my game with both feet. Then suddenly I came to a screeching halt. I had 4 listboxes in each window to fill and modify. Minimum 1 window, maximum 4 windows. This presented two problems.
I decided to build the windows in reverse order. This way, when the last window was drawn, they would already be in the correct order, and I wouldn't have to remap the 1st players window #handle. I had previously come up with a formula to cascade the windows. The demo enclosed shows how I decided to handle the problem with the listboxes. This demo also gives examples of the following:
With a simple modification in the sub noResize, (deleting the menu item "move"), you can also prevent the user from moving the window on the screen.
I hope someone finds this demo useful, and that it saves someone some time and headaches.
- Ken Lewis, Sr.
GLOBAL manip$, no.players, controlHndl$, roll.num, score, column, blank$, i, round
nomainwin 'not all of these global variables are used in this demo
no.players=4 'I have an initialization program that
plname$(1)="Brian" 'usually generates and passes player
plname$(2)="Tabitha" 'information to this program but I added
plname$(3)="Joe" 'these lines for this demo
plname$(4)="Barbara"
dim pl0$(23)
dim pl1$(23), pl1s$(23), pl1d$(23), pl1t$(23)
dim pl2$(23), pl2s$(23), pl2d$(23), pl2t$(23)
dim pl3$(23), pl3s$(23), pl3d$(23), pl3t$(23)
dim pl4$(23), pl4s$(23), pl4d$(23), pl4t$(23)
if no.players = 4 then manip$ = "1234" 'These strings are used to manipulate
if no.players = 3 then manip$ = "123" 'window position
if no.players = 2 then manip$ = "12"
if no.players = 1 then manip$ = "1"
player = 1
wBar=_SB_VERT 'used to hide listbox scrollbar
bShow=0 'used to hide listbox scrollbar
for x=1 to no.players
open "3ScoreCard.dat" for input as #fill 'this is where I fill the listboxes
for y=1 to 23 'I put the for/next loop outside the
select case x 'select/case to save a few lines of code
case 1
input #fill, pl1$(y),pl1s$(y),pl1d$(y),pl1t$(y)
case 2
input #fill, pl2$(y),pl2s$(y),pl2d$(y),pl2t$(y)
case 3
input #fill, pl3$(y),pl3s$(y),pl3d$(y),pl3t$(y)
case 4
input #fill, pl4$(y),pl4s$(y),pl4d$(y),pl4t$(y)
end select
next y
close #fill
next x
open "user32.dll" for dll as #user
for no.Windows=no.players to 1 step -1
WindowWidth = 380
WindowHeight = 445
UpperLeftX = (int((DisplayWidth-WindowWidth)/2))-((no.Windows-1)*20)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)-((no.Windows-1)*20)
ULx(no.Windows) = UpperLeftX
ULy(no.Windows) = UpperLeftY
select case no.Windows
case 4
listbox #play1.scorecard, pl4$(, Score, 5, 35, 160, 350 'this select case
listbox #play1.single, pl4s$(, Score, 163, 35, 50, 350 'ensures that I have
listbox #play1.double, pl4d$(, Score, 211, 35, 50, 350 'the correct listboxes
listbox #play1.triple, pl4t$(, Score, 259, 35, 50, 350 'in the correct window
case 3
listbox #play1.scorecard, pl3$(, Score, 5, 35, 160, 350 'I start with the last
listbox #play1.single, pl3s$(, Score, 163, 35, 50, 350 'window first
listbox #play1.double, pl3d$(, Score, 211, 35, 50, 350 'because I wont be
listbox #play1.triple, pl3t$(, Score, 259, 35, 50, 350 'remapping the handle
case 2
listbox #play1.scorecard, pl2$(, Score, 5, 35, 160, 350 'for player#1 window
listbox #play1.single, pl2s$(, Score, 163, 35, 50, 350
listbox #play1.double, pl2d$(, Score, 211, 35, 50, 350
listbox #play1.triple, pl2t$(, Score, 259, 35, 50, 350
case 1
listbox #play1.scorecard, pl1$(, Score, 5, 35, 160, 350
listbox #play1.single, pl1s$(, Score, 163, 35, 50, 350
listbox #play1.double, pl1d$(, Score, 211, 35, 50, 350
listbox #play1.triple, pl1t$(, Score, 259, 35, 50, 350
end select
graphicbox #play1.die1, 315, 5, 50, 50
graphicbox #play1.die2, 315, 87, 50, 50
graphicbox #play1.die3, 315, 169, 50, 50
graphicbox #play1.die4, 315, 251, 50, 50
graphicbox #play1.die5, 315, 333, 50, 50
button #play1.rollem, "Roll", roll, UL, 5, 5, 50, 25
button #play1.donem, "Done", nextPlayer, UL, 79, 5, 50, 25
button #play1.changem, "Change last score", removeScore, UL, 153, 5, 154, 25
button #play1.hold1, "Hold", hold, UL, 315, 58, 50, 25
button #play1.hold2, "Hold", hold, UL, 315, 140, 50, 25
button #play1.hold3, "Hold", hold, UL, 315, 222, 50, 25
button #play1.hold4, "Hold", hold, UL, 315, 304, 50, 25
button #play1.hold5, "Hold", hold, UL, 315, 386, 50, 25
TextboxColor$="white"
textbox #play1.triptot, 259, 386, 50, 25
TextboxColor$="palegray"
textbox #play1.posi,54,386,200,25
stylebits #play1.posi, _ES_RIGHT, 0, 0, 0
this.Window$="Player "+str$(no.Windows)
open this.Window$ for graphics_nsb as #play1
hlbox=hwnd(#play1.scorecard)
hlbox(no.Windows,1)=hlbox 'if we get the windows handle to each listbox
hlbox=hwnd(#play1.single) 'we can then remove the scrollbars
hlbox(no.Windows,2)=hlbox
hlbox=hwnd(#play1.double)
hlbox(no.Windows,3)=hlbox
hlbox=hwnd(#play1.triple)
hlbox(no.Windows,4)=hlbox
for x=1 to 4
hlbox=hlbox(no.Windows,x)
calldll #user, "ShowScrollBar",_ 'The ShowScrollBar function can be used
hlbox as ulong,_ 'to show or hide the controls scrollbars
wBar as short,_
bShow as boolean,_
ret as boolean
next x
print #play1, "trapclose [exit]"
print #play1, "down"
print #play1, "fill palegray; flush"
print #play1.die1, "fill white; flush"
print #play1.die2, "fill white; flush"
print #play1.die3, "fill white; flush"
print #play1.die4, "fill white; flush"
print #play1.die5, "fill white; flush"
print #play1.scorecard, "singleclickselect"
print #play1.scorecard, "font fixedsys 9"
print #play1.single, "singleclickselect"
print #play1.single, "font fixedsys 9"
print #play1.double, "singleclickselect"
print #play1.double, "font fixedsys 9"
print #play1.triple, "singleclickselect"
print #play1.triple, "font fixedsys 9"
print #play1.posi, "YOUR CURRENT SCORE"
print #play1.posi, "!disable"
agw(no.Windows) = hwnd(#play1)
aw(no.Windows)=getLegalGaurdian(agw(no.Windows))
T$ = plname$(no.Windows) + " (" + str$(trip.total(no.Windows)) + ")"
call showName aw(no.Windows), T$
call noResize aw(no.Windows)
if no.Windows>1 then 'here we build a handle variable
handle$="#play"+str$(no.Windows) 'and use it to remap the window #handle
maphandle #play1, handle$
end if
next no.Windows
call player.enable player
wait
[exit]
for wincount=no.players to 1 step -1 'closing the open windows is much easier
handle$="#play"+str$(wincount) 'now using handle variables
close #handle$
next wincount
close #user
end
sub showName h,T$
calldll #user, "SetWindowTextA", _
h as word, _
T$ as ptr, _
result as void
end sub
sub noResize h
calldll #user, "GetSystemMenu", _
h as word, 0 as word, hmenu as word
one.sysmenu.Restore=GetMenuItemID(hmenu,0)
one.sysmenu.Move=GetMenuItemID(hmenu,1) 'if you delete the move menu Item you can
one.sysmenu.Size=GetMenuItemID(hmenu,2) 'prevent the user from moving the window
one.sysmenu.Minimize=GetMenuItemID(hmenu,3)
one.sysmenu.Maximize=GetMenuItemID(hmenu,4)
one.sysmenu.sep1=GetMenuItemID(hmenu,5)
one.sysmenu.Close=GetMenuItemID(hmenu,6)
one.sysmenu.KillApps=GetMenuItemID(hmenu,7)
calldll #user, "DeleteMenu", hmenu as word, one.sysmenu.Minimize as word, _
_MF_BYCOMMAND as word, re as word
calldll #user, "DeleteMenu", hmenu as word, one.sysmenu.Maximize as word, _
_MF_BYCOMMAND as word, re as word
calldll #user, "DeleteMenu", hmenu as word, one.sysmenu.Size as word, _
_MF_BYCOMMAND as word, re as word
end sub
function GetMenuItemID(hmenu,index)
calldll #user, "GetMenuItemID", hmenu as word, index as word, GetMenuItemID as word
end function
sub player.enable player
for p = 1 to no.players
if p = player then
enabled = 1
else
enabled = 0
end if
for e = 1 to 4
h = hlbox(p,e)
calldll #user, "EnableWindow", _
h as word, _
enabled as ushort, _
result as ushort
next e
next p
for x = no.players to 1 step -1
m = val(mid$(manip$, x, 1))
h = aw(m)
xPos = ULx(x)
yPos = ULy(x)
toTop = 0
flags = _SWP_NOSIZE
calldll #user, "SetWindowPos", _
h as ushort, _
toTop as short, _
xPos as short, _
yPos as short, _
0 as short, _
0 as short, _
flags as ushort, _
result as void
next x
if no.players<>1 then
manip$=right$(manip$,len(manip$)-1)+left$(manip$,1) 'This is where we cut up and
end if 'rearrange manip$ for the next
end sub 'players window to be on top
function getLegalGaurdian(ht)
CallDLL #user, "GetParent", _
ht AS word, _
getLegalGaurdian AS word
end function
Copy the text below and save as "3ScoreCard.dat".
Aces...............,.....,.....,.....,Twos...............,.....,.....,..... Threes.............,.....,.....,.....,Fours..............,.....,.....,..... Fives..............,.....,.....,.....,Sixes..............,.....,.....,..... - ,- ,- ,- ,Sub Total..........,.....,.....,..... Bonus..............,.....,.....,.....,Total Top..........,.....,.....,..... - ,- ,- ,- ,3 of a Kind........,.....,.....,..... 4 of a Kind........,.....,.....,.....,Full House.........,.....,.....,..... Sm Straight........,.....,.....,.....,Lg Straight........,.....,.....,..... Chance.............,.....,.....,.....,Yaght..............,.....,.....,..... Yaght Bonus........,.....,.....,.....,- ,- ,- ,- Total Bottom.......,.....,.....,.....,Total Top..........,.....,.....,..... Column Total.......,.....,.....,.....